package com.grass.cropTradingCenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grass.cropTradingCenter.common.OutputException;
import com.grass.cropTradingCenter.common.PageResult;
import com.grass.cropTradingCenter.mapper.GoodsMapper;
import com.grass.cropTradingCenter.pojo.entity.Goods;
import com.grass.cropTradingCenter.pojo.entity.MiniShop;
import com.grass.cropTradingCenter.pojo.entity.UserOrder;
import com.grass.cropTradingCenter.pojo.po.QueryImage;
import com.grass.cropTradingCenter.pojo.vo.shop.GoodsListVo;
import com.grass.cropTradingCenter.pojo.vo.shop.OrderListVo;
import com.grass.cropTradingCenter.service.*;
import com.grass.cropTradingCenter.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author zipan
* @description 针对表【goods】的数据库操作Service实现
* @createDate 2024-03-05 13:30:38
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService{

    @Autowired
    MiniShopUserService miniShopUserService;

    @Autowired
    MiniShopService miniShopService;

    @Autowired
    ImageService imageService;

    @Autowired
    GoodsPackageService goodsPackageService;

    @Autowired
    UserOrderService userOrderService;

    @Override
    public PageResult<Goods> goodsList(GoodsListVo vo) {
        Integer shopId = getShopId();
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getShopId, shopId);
        if (StringUtils.isNoneBlank(vo.getKey())) {
            queryWrapper.and((wrapper) -> {
                wrapper.like(Goods::getGoodsName, vo.getKey())
                        .or().like(Goods::getGoodsDescription, vo.getKey());
            });
        }
        if (vo.getState() != 10) {
            queryWrapper.eq(Goods::getCheckFlag, vo.getState());
        }
        if (vo.getSellFlag() != 10) {
            queryWrapper.eq(Goods::getSellFlag, vo.getSellFlag());
        }

        Page<Goods> page = this.page(new Page<>(vo.getPageNum(), vo.getPageSize()), queryWrapper);


        page.getRecords().forEach(goods -> {
            goods.setDetailImages(imageService.getImageList(new QueryImage(goods.getGoodsId(), 1)));
            goods.setPackages(goodsPackageService.getPackageList(goods.getGoodsId()));
        });
        return PageResult.ofPage(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addOrUpdateGoods(Goods vo) {
        // 说明是更新请求
        if (vo.getGoodsId() != null && vo.getGoodsId() != 0) {
            Integer goodsId = vo.getGoodsId();
            imageService.removeImages(new QueryImage(goodsId, 1));
            goodsPackageService.removePackagesByGoodsId(goodsId);
            this.updateById(vo);
        } else {
            vo.setShopId(getShopId());
            this.save(vo);// 插入请求
        }
        imageService.saveImages(new QueryImage(vo.getGoodsId(), 1), vo.getDetailImages());
        goodsPackageService.savePackages(vo.getGoodsId(), vo.getPackages());
    }

    @Override
    public PageResult<UserOrder> orderList(OrderListVo vo) {
        int shopId = getShopId();
        LambdaQueryWrapper<UserOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserOrder::getShopId, shopId)
                .eq(UserOrder::getDelFlag, 0);
        if (StringUtils.isNoneEmpty(vo.getKey())) {
            queryWrapper.and(wrapper -> {
                wrapper.like(UserOrder::getGoodsName, vo.getKey())
                        .or().like(UserOrder::getOrderCode, vo.getKey())
                        .or().like(UserOrder::getAddressName, vo.getKey());
            });
        }
        if (vo.getOrderType() != 0) {
            queryWrapper.eq(UserOrder::getOrderType, vo.getOrderType());
        }
        queryWrapper.orderByDesc(UserOrder::getId);
        Page<UserOrder> page = userOrderService.page(new Page<>(vo.getPageNum(), vo.getPageSize()), queryWrapper);

        return PageResult.ofPage(page);
    }

    @Override
    public void confirmFH(Integer id) {
        UserOrder order = userOrderService.getById(id);
        if (order.getOrderType() == 2) {
            order.setOrderType(3);
            userOrderService.updateById(order);
            return;
        }
        throw new OutputException("操作失败，请联系管理员");
    }

    @Override
    public void confirmTK(Integer id) {
        UserOrder order = userOrderService.getById(id);
        if (order.getOrderType() == 10) {
            order.setOrderType(12);
            userOrderService.updateById(order);
            return;
        }
        throw new OutputException("操作失败，请联系管理员");
    }

    @Override
    public Integer getGoodsNumByShopId(Integer shopId) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getShopId, shopId)
                .eq(Goods::getCheckFlag, 1);

        return this.count(queryWrapper);
    }

    private int getShopId() {
        int shopUserId = SecurityUtils.getShopUserId();
        List<MiniShop> shops = miniShopService.list(new LambdaQueryWrapper<MiniShop>().eq(MiniShop::getShopUserId, shopUserId));
        if (shops.isEmpty()) return 0;
        return shops.get(0).getShopId();
    }
}




